﻿using Entitas;

namespace Script.Systems
{
    public class PhysicSystem : IExecuteSystem
    {
        private readonly Contexts   myContexts;
        private readonly PhysicComp physicComp;

        public PhysicSystem (Contexts contexts)
        {
            myContexts = contexts;
            physicComp = contexts.physics.physicComp;
        }
        
        public void Execute ()
        {
            foreach (var collisionInfo in physicComp.collisionInfoList)
            {
                var source = myContexts.game.GetEntityWithId (collisionInfo.sourceId);
                var other  = myContexts.game.GetEntityWithId (collisionInfo.otherId);
                
                if (source.isBulletTag)
                {
                    if (other.isEnemyTag)
                    {
                        other.isDestoryComp  = true;
                        source.isDestoryComp = true;
                    }
                }
            }
            physicComp.collisionInfoList.Clear ();
        }
    }
}